<%
Class aspwebcache
	Private store, content
	
	Private Sub Class_Initialize()
	End Sub
	
	Public Sub init(storeType)
		Select Case storeType
			Case "application object"
				Set store = new aspwebcache_applicationObject_store
			Case Else
				'application obeject is the default storage engine
				Set store = new aspwebcache_applicationObject_store
		End Select
	End Sub	
	
	Private Sub Class_Terminate()
		Set store = Nothing
	End Sub
	
	Public Function run(url, mode)
		Select Case mode
			Case "refresh"
				'Refreshes current resource from cache
				content = fetch(addURLParam(url, "aspwebcache", "bypass"))
				store.setContent url, content
				run = store.getContent(url)
			Case "reset"
				'Removes current resource from cache
				store.remove url
				run = url + " removed"
			Case "stats"
				run = "number of pages stored: " + cstr(store.count)
			Case "refresh-all"
			Case "reset-all"
				'Empties cache
				store.removeAll
				run = "number of pages stored: " + cstr(store.count)
			Case Else
				'Caches current resource
				timestamp = store.getAttribute(url,"ts")
				'Check if cache expire time has passed
				If datediff("n", timestamp, now()) >= timeout Then'Cache expire time has passed
					content = fetch(addURLParam(url, "aspwebcache", "bypass"))
					store.setContent url, content
					run = store.getContent(url)
				ElseIf datediff("n", timestamp, now()) >=(0.7*timeout) Then'Cache expire time is about to pass
					run = store.getContent(url)
					content = fetch(addURLParam(url, "aspwebcache", "bypass"))
					store.setContent url, content
				Else'Cache expire time has not passed yet
					run = store.getContent(url)
				End If
		End Select
	End Function

	Public Function contains(str1, str2)
		'Returns true if str1 contains an instance of str2
		contains = cbool(instr(1, str1, str2, 1))
	End Function
	
	Public Function addURLParam(url, param, value)
		'Adds specified parameter with value to URL
		If contains(url, "?") Then
			addURLParam = url & "&" & Server.URLEncode(param) & "=" & Server.URLEncode(value)
		Else
			addURLParam = url & "?" & Server.URLEncode(param) & "=" & Server.URLEncode(value)
		End If
	End function
	
	Function removeURLParam(url, param)
		'Removes specified parameter (if exists in URL) from URL.
		Dim RegEx, x
		Set RegEx = New RegExp
		With RegEx
			.Pattern = "&?" & param & "=\w*"
			.IgnoreCase = true
			.Global = True	
			x = .Replace(url, "")
		End With
		x = replace(x, "?&", "?")
		If right(x, 1) = "?" Then x = left(x, len(x)-1)
		removeURLParam = x
	End Function

	Public Function fetch(url)
		'Gets the resource that resides in specified url
		dim xml
		Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
		xml.Open "GET", url, False
		xml.Send
		fetch = xml.responseText
		Set xml = Nothing
	End Function

End Class 
%>